﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace HY.BLL
{
  public   class BLLfun
    {

      public static DataTable get_search(string tabname )
      {
          string sqltxt = "select [字段名],[字段说明]  FROM [vi_get_table] "+
              " where [字段名]<>'sid' and [字段说明] is not null and [字段说明]<>'' and [表名]='" + tabname + "' ";
          return DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
      }

      public static DataTable get_where()
      {
          string sqltxt = "select * from vi_sys_where ";
          return DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
      }

      public static string get_company(string sbh)
      {
          string sqltxt = "select  pvendor FROM [pq_ht] where pbh='"+sbh+"' ";
          DataTable dd= DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
          string rstring="";
          if (dd.Rows.Count > 0)
              rstring = dd.Rows[0][0].ToString();

          return rstring;
      }

      /// <summary>
      /// 派遣员工-在职状态
      /// </summary>
      /// <param name="val"></param>
      /// <returns></returns>
      public static string Get_pepo_status(String val)
      { 
          string  str="";

          if (val == "0") str = "在职";
          if (val == "1") str = "离职";
          if (val == "2") str = "待职";

          return str;
      }

      public static string GetAge(DateTime dtBirthday, DateTime dtNow)
        {

            string  strAge      = string.Empty;                         // 年龄的字符串表示

            int     intYear     = 0;                                    // 岁

            int     intMonth    = 0;                                    // 月

            int     intDay      = 0;                                    // 天

 


            // 计算天数

            intDay = dtNow.Day - dtBirthday.Day;

            if (intDay < 0)

            {

                dtNow = dtNow.AddMonths(-1);

                intDay += DateTime.DaysInMonth(dtNow.Year, dtNow.Month);

            }

            

            // 计算月数

            intMonth = dtNow.Month - dtBirthday.Month;

            if (intMonth < 0)

            {

                intMonth += 12;

                dtNow = dtNow.AddYears(-1);

            }

            

            // 计算年数

            intYear = dtNow.Year - dtBirthday.Year;

            

            // 格式化年龄输出

            if (intYear >= 1)                                            // 年份输出

            {

                strAge = intYear.ToString() + "岁";

            }

            

            if (intMonth > 0 && intYear <= 5)                           // 五岁以下可以输出月数

            {

                strAge += intMonth.ToString() + "月";

            }

            

            if (intDay >= 0 && intYear < 1)                              // 一岁以下可以输出天数

            {

                if (strAge.Length == 0 || intDay > 0)

                {

                    strAge += intDay.ToString() + "日";

                }

            }

            

            return strAge;

        }


      public static string DateDiff(DateTime DateTime1, DateTime DateTime2)
      { 
          string dateDiff = null; 
          try { 
              TimeSpan ts1 = new TimeSpan(DateTime1.Ticks); 
              TimeSpan ts2 = new TimeSpan(DateTime2.Ticks); 
              TimeSpan ts = ts1.Subtract(ts2).Duration(); 
           //   dateDiff = ts.Days.ToString() + "天" + 
            //      ts.Hours.ToString() + "小时" +
             //     ts.Minutes.ToString() + "分钟" + 
             //     ts.Seconds.ToString() + "秒";
              dateDiff = ts.Days.ToString() + "." +
                ts.Hours.ToString() ; 

          } catch { } 
          return dateDiff;
      }

      /// <summary>
      /// 考勤时间类型
      /// </summary>
      /// <returns></returns>
      public static string gettimetype(string ty, DateTime tt, string am)
      {
          string val="正常";
          int sh,nh,sm,nm;
       
           DateTime dt =tt;  //当前时间
         
         //  string tem=dt.ToString("yyyy-MM-dd") +" "+ am;  //考勤标准时间
      
          // DateTime stddt=DateTime.Parse(tem);
            string[] stdtime = am.Split(':');

         
          //时间
           sh = int.Parse(stdtime[0].ToString());
           nh =int.Parse(  dt.Hour.ToString());

          //分钟
           sm = int.Parse(stdtime[1].ToString());
           nm =int.Parse(  dt.Minute.ToString());


           if(nh-sh>2||sh-nh>2) val="非工作时间";
         
           if (ty == "上班")
           if(nh==sh&nm-sm>0) val="迟到";

           if (ty=="下班")
           if(nh==sh&nm-sm<0) val="早退";

          return val;
      }

      //DateTime GetTime(DateTime dateTrue, int intType)
      //{
      //    DateTime dateCalculate = new DateTime();

      //    if (intType == 0)        //上班
      //    {
      //        if (dateTrue.Minute > 0 && dateTrue.Minute <= 30)
      //            dateCalculate = DateTime.Parse(dateTrue.ToString("yyyy-MM-dd HH:30:00"));
      //        if (dateTrue.Minute > 31 && dateTrue.Minute <= 59)
      //            dateCalculate = DateTime.Parse(dateTrue.AddHours(1).ToString("yyyy-MM-dd HH:00:00"));
      //    }
      //    else                    //下班
      //    {
      //        if (dateTrue.Minute > 0 && dateTrue.Minute <= 30)
      //            dateCalculate = DateTime.Parse(dateTrue.ToString("yyyy-MM-dd HH:00:00"));
      //        if (dateTrue.Minute > 31 && dateTrue.Minute <= 59)
      //            dateCalculate = DateTime.Parse(dateTrue.AddHours(1).ToString("yyyy-MM-dd HH:30:00"));
      //    }

      //    return dateCalculate;
      //}



      public static void up_sales_num_add(string ty, string bh)
      {

          string sqltxt = "update "+ty+" set cnum=cnum+1 where ";
          if (ty=="schoole")
              sqltxt=sqltxt+" schbh=N'"+bh+"'";

         if(ty=="company")
             sqltxt=sqltxt+" cbh=N'"+bh+"'";

         if(ty=="zj")
            sqltxt = sqltxt + " zbh=N'" + bh + "'";

         DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);

          //更新合同次数
         DAL.DbHelperSQL.ExecuteNonQuery("update company_fav set cnum=cnum+1 where cbh=N'"+bh+"'");

      }

      public static void up_sales_num_del(string ty, string bh)
      {

          string sqltxt = "update " + ty + " set cnum=cnum-1 where ";
          if (ty == "schoole")
              sqltxt = sqltxt + " schbh=N'" + bh + "'";

          if (ty == "company")
              sqltxt = sqltxt + " cbh=N'" + bh + "'";

          if (ty == "zj")
              sqltxt = sqltxt + " zbh=N'" + bh + "'";

          DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);
          //更新合同次数
          DAL.DbHelperSQL.ExecuteNonQuery("update company_fav set cnum=cnum-1 where cbh='" + bh + "'");

      }

      /// <summary>
      /// 自动删除过期的收藏
      /// </summary>
      public static void delete_sales_get()
      {
          DataTable dt = DAL.DbHelperSQL.SqlDataAdapter("select * from company_fav where  DATEDIFF(day,getdate(),outdate)<=3 and (cnum<1 or cnum is null)");
          string sqltxt = "",val="",guest="";
          for (int i = 0; i < dt.Rows.Count; i++)
          {
              guest = dt.Rows[i]["company"].ToString();
              val = dt.Rows[i]["sales"].ToString();
              sqltxt = "insert into [msg_list] values(0,N'admin',N'"+val+"',N'客户信息到期提醒',N'如果仍然不能签定合同，"+guest+"-将到期释放到公共区',getdate(),0,'',1) ";
                           
              DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);
          } 
          
          sqltxt = "delete company_fav where outdate<getdate() and (cnum<1 or cnum is null)";

      }
      /// <summary>
      /// 获取用户名
      /// </summary>
      public static string getname(string lgid)
      {
          string sqltxt = "select user_name from sys_user where lgid='" + lgid + "' ";
          string val = "";
          DataTable s = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
          if (s.Rows.Count > 0)
          {

              val = s.Rows[0][0].ToString();
          }

          return val;
      }

      public static void get_process(string rbh, string nm,string stype)
      {

          //取请假单对应的流程
          string val = BLL.oa.leave_sub.get_eqau_val("formname='"+stype+"'");

          //取流程
          string spr = "";


          //取审批流程
          string sql = "  SELECT *  FROM [pro_list] where [retitle]='" + val + "'";

          DataTable dk = DAL.DbHelperSQL.SqlDataAdapter(sql);

          BLL.HR.sys_user us = new HR.sys_user();

          //取第一个
          for (int i = 0; i < dk.Rows.Count; i++)
          {

              //取审批人- 如果审批人为空,取当前部门角色
              if (dk.Rows[i]["renm"].ToString() == "")
              {
                  DataTable ds = us.GetList("role='" + dk.Rows[i]["role"].ToString() +  "'").Tables[0];

                                       // "' and dept=(select dept from sys_user where lgid='" + nm + "')
                    
                  if (ds.Rows.Count > 0)
                  {
                      spr = ds.Rows[0]["user_name"].ToString();
                  }

              }
              else
                  spr = dk.Rows[i]["renm"].ToString();

              if (spr != "")
                  DAL.DbHelperSQL.ExecuteNonQuery("insert into  leave_sub (reviewBH ,reviewnm,revieword) " +
                      " values(N'" + rbh + "',N'" + spr + "'," + (i + 1).ToString() + ")");

          }


      }
    }
}
